

// pages/mistake/mistake.js
const app = getApp({})
Page({
  data: {
     info:[],
     infoTemp:[],
     select:['A','B','C','D'],
     showFlag:[],
     closeFlag:true,
     option1: [
        { text: '全部收藏', value: 0 },
      ],
      option2: [
        { text: '不限日期', value: 0 },
        { text: '近一周收藏', value: 1,day:7 },
        { text: '大于一周收藏', value: 2,day:7 },
      ],
      value1: 0,
      value2: 0
  },
  onLoad(){
    this.get_user_collection()
  },
  /* 筛选 */
  onConfirmOne(e){
      console.log(e);
      var index =e.detail
      this.setData({
          value1:index
      })
      if(index==0){
          if(this.data.value2==0){
            this.setData({
                info:this.data.infoTemp
            })
          }else{
            var tempInfoRow=this.data.infoTemp
            const currentTime = new Date().getTime(); // 获取当前时间的时间戳
            const sevenDaysInMillis = 7 * 24 * 60 * 60 * 1000;

            const filteredArray = tempInfoRow.filter(item => {
                const itemTime = new Date(item.create_at).getTime(); // 解析时间字符串，注意iOS兼容性需要替换'-'
                if(this.data.value2==1){
                    return (currentTime - itemTime) < sevenDaysInMillis;
                }else{
                    return (currentTime - itemTime) > sevenDaysInMillis;
                }
            });
            console.log(filteredArray);
            this.setData({
                info:filteredArray
            });
          }
      }else{
          if(this.data.value2==0){
            var tempInfo = this.data.infoTemp
            const tempArray = tempInfo.filter(item=>item.cotitle==this.data.option1[index].text)
            this.setData({
                info:tempArray
            })
          }else{
            var tempInfoRow=this.data.infoTemp
            const currentTime = new Date().getTime(); // 获取当前时间的时间戳
            const sevenDaysInMillis = 7 * 24 * 60 * 60 * 1000;

            const filteredArray = tempInfoRow.filter(item => {
                const itemTime = new Date(item.create_at).getTime(); // 解析时间字符串，注意iOS兼容性需要替换'-'
                console.log(currentTime - itemTime,sevenDaysInMillis,itemTime,);
                if(this.data.value2==1){
                    return (currentTime - itemTime) < sevenDaysInMillis && item.cotitle==this.data.option1[e.detail].text;
                }else{
                    return (currentTime - itemTime) > sevenDaysInMillis && item.cotitle==this.data.option1[e.detail].text;
                }
            });
            console.log(filteredArray);
            this.setData({
                info:filteredArray
            });
          }
         
      }
  },
  /* 筛选日期 */
  onConfirmTwo(e){
    console.log(e);
    this.setData({
        value2:e.detail
    })
    if(e.detail==0){
        if(this.data.value1==0){
            this.setData({
                info:this.data.infoTemp
            })
        }else{
            var tempInfo = this.data.infoTemp

            console.log(this.data.option1);
            console.log(this.data.value1);
            const tempArray = tempInfo.filter(item=>item.cotitle==this.data.option1[this.data.value1].text)
            this.setData({
                info:tempArray
            })
        }
    }else{
        if(this.data.value1==0){
            /* 只筛选日期 */
            var tempInfoRow=this.data.infoTemp
            const currentTime = new Date().getTime(); // 获取当前时间的时间戳
            const sevenDaysInMillis = 7 * 24 * 60 * 60 * 1000;

            const filteredArray = tempInfoRow.filter(item => {
                const itemTime = new Date(item.create_at).getTime(); // 解析时间字符串，注意iOS兼容性需要替换'-'
                console.log(currentTime - itemTime,sevenDaysInMillis,itemTime,);
                if(e.detail==1){
                    return (currentTime - itemTime) < sevenDaysInMillis;
                }else{
                    return (currentTime - itemTime) > sevenDaysInMillis;
                }
            });
            console.log(filteredArray);
            this.setData({
                info:filteredArray
            });
        }else{
            /* 筛选类型和日期 */
            /* 只筛选日期 */
            var tempInfoRow=this.data.infoTemp
            const currentTime = new Date().getTime(); // 获取当前时间的时间戳
            const sevenDaysInMillis = 7 * 24 * 60 * 60 * 1000;

            const filteredArray = tempInfoRow.filter(item => {
                const itemTime = new Date(item.create_at).getTime(); // 解析时间字符串，注意iOS兼容性需要替换'-'
                console.log(currentTime - itemTime,sevenDaysInMillis,itemTime,);
                if(e.detail==1){
                    return (currentTime - itemTime) < sevenDaysInMillis && item.cotitle==this.data.option1[e.detail].text;
                }else{
                    return (currentTime - itemTime) > sevenDaysInMillis && item.cotitle==this.data.option1[e.detail].text;
                }
            });
            console.log(filteredArray);
            this.setData({
                info:filteredArray
            });
        }
    }
  },
  /* 获得用户的收藏 */
  get_user_collection(){
    wx.showLoading({
        title: '加载中',
      })
      wx.request({
        url:app.globalData.baseUrl + '/user/get_collection',
        method:"POST",
        data:{
            userid:wx.getStorageSync('userInfo').id
        },success:(res)=>{
            wx.hideLoading()
            console.log(res);
            if(res.data.success==true){
                var temp = res.data.data
                temp.forEach(element => {
                   element.create_at=this.formatGMTTime(element.create_at)
                });
                this.setData({
                    info:temp,
                    infoTemp:temp
                })
                console.log("信息",this.data.info);
                this.filterUniqueTitles(temp)
            }else{
                wx.hideLoading()
                console.log(res.data);
            }
        },fail:(res)=>{
            wx.hideLoading()
            wx.showToast({
              title: 'serve error',
              icon:"none"
            })
        }
      })
  },
  formatGMTTime(gmtString) {
    // 解析GMT时间字符串为Date对象
    const date = new Date(gmtString);
  
    // 获取年、月、日、小时、分钟和秒
    const year = date.getFullYear();
    const month = date.getMonth() + 1;  // getMonth()返回的月份是从0开始的，所以需要+1
    const day = date.getDate();
    const hours = date.getHours();
    const minutes = date.getMinutes();
    const seconds = date.getSeconds();
  
    // 构建目标格式的时间字符串
    const formattedTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  
    return formattedTime;
  } ,
  bindDelWith(e){
    var index = e.currentTarget.dataset.index
    console.log(index);
    var id = this.data.info[index].id
    wx.showModal({
      title: '确定删除？',
      content: '点击确定删除信息',
      complete: (res) => {
        if (res.cancel) {
          wx.showToast({
            title: '取消删除',
          })
        }
        if (res.confirm) {
            wx.request({
                url: app.globalData.baseUrl+'/user/collect',
                method:"POST",
                data:{
                    id:id
                },success:(res)=>{
                    wx.hideLoading()
                    if(res.data.success==true){
                      wx.showToast({
                          title: '删除成功',
                        })
                        this.data.info.splice(index,1)
                        this.data.infoTemp.splice(index,1)
                        this.setData({
                            info:this.data.info,
                            infoTemp:this.data.infoTemp
                        })
                    }
                },fail(){
                    wx.showToast({
                      title: '删除失败',
                      icon:"none"
                    })
                }
              })
        }
      }
    })
    
   
  },
  /* 构建options1 */
  filterUniqueTitles: function(option) {
    console.log(option);
    let uniqueTitles = [];
    let lookup = new Set();  // 使用 Set 来检测唯一性

    option.forEach((item, index) => {
      if (!lookup.has(item.cotitle)) {
        lookup.add(item.cotitle);
        uniqueTitles.push({ text: item.cotitle, value: index+1 });
      }
    });
    this.setData({
      option1:this.data.option1.concat(uniqueTitles) 
    });
    console.log("options信息",this.data.option1);
  },
  filterByDate: function(option) {
    const { originalArray } = this.data;
    const currentDate = new Date(); // 当前时间
    const sevenDaysAgo = new Date(currentDate.getTime() - (7 * 24 * 60 * 60 * 1000)); // 七天前的时间

    const filteredArray = originalArray.filter(item => {
      const itemDate = new Date(item.time.replace(/-/g, '/')); // 解析时间字符串，注意iOS兼容性需要替换'-'
      return itemDate < sevenDaysAgo; // 比较日期
    });
    this.setData({
      filteredArray
    });
  }
})